package com.bonc.jedisCluster.connectionHandler;

import java.util.Set;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisClusterHostAndPortMap;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisSlotBasedConnectionHandler;
import redis.clients.jedis.exceptions.JedisNoReachableClusterNodeException;

public class MyJedisSlotBasedConnectionHandler extends JedisSlotBasedConnectionHandler {
	  public MyJedisSlotBasedConnectionHandler(Set<HostAndPort> nodes,
	      final GenericObjectPoolConfig poolConfig, int timeout) {
	    this(nodes, poolConfig, timeout, timeout);
	  }

	  public MyJedisSlotBasedConnectionHandler(Set<HostAndPort> nodes,
	      final GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout) {
	    super(nodes, poolConfig, connectionTimeout, soTimeout, null);
	  }

	  public MyJedisSlotBasedConnectionHandler(Set<HostAndPort> nodes, GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String password) {
	    super(nodes, poolConfig, connectionTimeout, soTimeout, password);
	  }

	  public MyJedisSlotBasedConnectionHandler(Set<HostAndPort> nodes, GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String password, String clientName) {
	    super(nodes, poolConfig, connectionTimeout, soTimeout, password, clientName);
	  }

	  public MyJedisSlotBasedConnectionHandler(Set<HostAndPort> nodes, GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String password, String clientName,
	                                         boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap portMap) {
	    super(nodes, poolConfig, connectionTimeout, soTimeout, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap);
	  }
	  /**
	   * 获取cache中的slots集合，得到slot映射的JedisPool，同一个节点上的slot映射到同一个JedisPool
	 * @param slot
	 * @return
	 */
	public JedisPool getJedisPoolFromSlot(int slot) {
		  JedisPool connectionPool = cache.getSlotPool(slot);
		  if (connectionPool != null) {
			  return connectionPool;
		  } else {
			  renewSlotCache();
			  connectionPool = cache.getSlotPool(slot);
			  if (connectionPool != null) {
				  return connectionPool;
			  } else {
				  throw new JedisNoReachableClusterNodeException("No reachable node in cluster for slot "+slot);
			  }
		  }
	  }
}
